package com.chuyu.cy.controller;

import com.chuyu.cy.contants.ResponseCodeEnum;
import com.chuyu.cy.contants.Result;
import com.chuyu.cy.entity.dto.RecommendDTO;
import com.chuyu.cy.entity.dto.UserDTO;
import com.chuyu.cy.entity.vo.UserVo;
import com.chuyu.cy.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author 周小白
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController {

    @Resource
    private UserService userService;



    /**
     * 添加用户
     * @param userDTO 用户信息
     * @return 添加结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加用户")
    public Result<UserVo> addUser(@RequestBody UserDTO userDTO) {
        Result<UserVo> result = new Result<>();

        if (userDTO.getNickName() == null || userDTO.getNickName().isEmpty()) {
            return result.failure("用户名不能为空");
        }
        if (userDTO.getStuId() == null || userDTO.getStuId().isEmpty()) {
            return result.failure("学号不能为空");
        }

        UserVo userVo = userService.addUser(userDTO);
        if (userVo.getId() == null) {
            return result.failure("添加失败");
        }

        return result.success(userVo);
    }





    /**
     * 获取用户信息
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/info/{id}")
    @Operation(summary = "获取用户信息")
    public Result<UserVo> getUserInfo(@PathVariable Integer id) {
        Result<UserVo> result = new Result<>();

        if (id == null) {
            return result.failure("用户id不能为空");
        }

        UserVo userVo = userService.getUserInfo(id);
        if (userVo == null) {
            return result.failure("用户不存在");
        }

        return result.success(userVo);
    }





    /**
     * 更新用户信息
     * @param userDTO 用户信息
     * @return 更新结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    public Result updateUserInfo(@RequestBody UserDTO userDTO) {
        Result result = new Result();

        if (userDTO.getId() == null) {
            return result.failure("用户id不能为空");
        }

        Boolean updated = userService.updateUserInfo(userDTO);
        if (!updated) {
            return result.failure("更新失败");
        }

        return result.success();
    }




    /**
     * 注销用户
     * @param id 用户id
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "注销用户")
    public Result deleteUser(@PathVariable("id") Integer id) {
        Result result = new Result();

        if (id == null) {
            return result.failure("用户id不能为空");
        }

        Boolean deleted = userService.deleteUser(id);
        if (!deleted) {
            return result.failure("注销失败");
        }

        return result.success();
    }





    /**
     * 推荐用户
     * @param recommendDTO 推荐要求
     * @return the result
     */
    @PostMapping("/recommend")
    @Operation(summary = "推荐用户")
    public Result<UserVo> recommend(@RequestBody RecommendDTO recommendDTO) {
        Result<UserVo> result = new Result<>();

        if (recommendDTO == null) {
            return result.failure(ResponseCodeEnum.NO_PARAM);
        }
        if (recommendDTO.getUserId() == null) {
            return result.failure("用户id不能为空");
        }

        UserVo userVo = userService.recommend(recommendDTO);
        if (userVo == null) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(userVo);
    }




    /**
     * 根据学号获取用户信息
     * @param stuId 学号
     * @return 用户信息
     */
    @GetMapping("/info")
    @Operation(summary = "根据学号获取用户信息")
    public Result<UserVo> getUserInfoByStuId(@RequestParam("stuId") String stuId) {
        Result<UserVo> result = new Result<>();

        if (stuId == null || stuId.isEmpty()) {
            return result.failure("学号不能为空");
        }

        UserVo userVo = userService.getUserInfoByStuId(stuId);
        if (userVo == null) {
            return result.failure("用户不存在");
        }

        return result.success(userVo);
    }
}
